package com.jianz.campus_markets.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectById;
import com.baomidou.mybatisplus.core.injector.methods.SelectList;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jianz.campus_markets.domain.ResponseResult;
import com.jianz.campus_markets.domain.entity.User;
import com.jianz.campus_markets.domain.vo.UserInfoVo;
import com.jianz.campus_markets.mapper.UserMapper;
import com.jianz.campus_markets.service.UserService;
import com.jianz.campus_markets.utils.BeanCopyUtils;
import com.jianz.campus_markets.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Jianz
 * @version 1.0
 * @Description
 * @Email jianz8153.gmail.com
 * @Date 2023/11/22 20:31
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public ResponseResult userInfo() {
        //获取当前用户id
        Long userId = SecurityUtils.getUserId();
        //根据用户id查询用户信息
        User user = getById(userId);
        //封装成UserInfoVo
        UserInfoVo vo = BeanCopyUtils.copyBean(user,UserInfoVo.class);
        return ResponseResult.okResult(vo);

    }

    @Override
    public ResponseResult updateUserInfo(User user) {
          String encodePassword = passwordEncoder.encode(user.getPassword());
          user.setPassword(encodePassword);
       baseMapper.updateById(user);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult otherUserInfo(Long id) {
        //根据用户id查询用户信息
        User user = getById(id);
        //封装成UserInfoVo
        UserInfoVo vo = BeanCopyUtils.copyBean(user,UserInfoVo.class);
        return ResponseResult.okResult(vo);
    }

    @Override
    public ResponseResult userSearch(String condition) {

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getNickName,condition)
                .or()
                .like(User::getUserName,condition);
        List<User> list = getBaseMapper().selectList(queryWrapper);
        return ResponseResult.okResult(list);
    }

}
